package com.healthcloud.controller;

import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.healthcloud.entity.MsgVo;
import com.healthcloud.entity.testresult.TestResultDto;
import com.healthcloud.service.TestresultService;
import com.healthcloud.utils.ApiRes;
import com.healthcloud.utils.DateUtils;
import com.healthcloud.utils.PageParam;
import com.healthcloud.utils.RRException;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Calendar;
import java.util.List;

/**
 * @author zhangjie
 */
@Api("检测记录")
@RestController
@RequestMapping("/mp/test/result")
public class TestResultController {

    @Autowired
    private TestresultService testResultService;

    @ApiOperation(value = "检测图表列表")
    @GetMapping("/list")
    public ApiRes listChart(TestResultDto testResultDto, PageParam pageParam) {
        List list = testResultService.findList(testResultDto, pageParam);
        int count = testResultService.countFindList(testResultDto);

        JSONObject o = JSONObject.parseObject(JSONObject.toJSONString(new MsgVo(list, count, count,pageParam.getsEcho()), SerializerFeature.WriteDateUseDateFormat));
        o.put("startDate", DateUtils.beforeDay(testResultDto.getDate()));
        o.put("endDate", DateUtils.getDay(Calendar.DATE, 0));
        return new ApiRes(o);
    }

    @ApiOperation(value = "血糖检测列表查询")
    @GetMapping("/patientBsTests")
    public MsgVo patientTests(TestResultDto testResultDto, PageParam pageParam) {
        testResultDto.setTestKindCode("105");
        List list = testResultService.patientBsTests(testResultDto, pageParam);
        int count = testResultService.coutPatientBsTests(testResultDto);
        return new MsgVo(list, count, count,pageParam.getsEcho());
    }

    @ApiOperation(value = "血压检测列表查询")
    @GetMapping("/patientBpTests")
    public MsgVo patientBpTests(TestResultDto testResultDto, PageParam pageParam) {
        testResultDto.setTestKindCode("104");
        List list = testResultService.patientBpTests(testResultDto, pageParam);
        int count = testResultService.countPatientBpTests(testResultDto);
        return new MsgVo(list, count, count,pageParam.getsEcho());
    }

    @ApiOperation(value = "血糖数据修改")
    @PostMapping("/updateBs")
    public ApiRes updateBs(@ApiParam(required = true, name = "id")
                      @RequestParam("id") Integer id,
                      @ApiParam(required = true, name = "testItemCode", value = "105001:空腹血糖 105002:餐后两小时")
                      @RequestParam("testItemCode") String testItemCode) {
        String testItemName;
        String preCode = "105001";
        String afterCode = "105002";
        if (preCode.equals(testItemCode)) {
            testItemName = "空腹血糖";
        } else if (afterCode.equals(testItemCode)) {
            testItemName = "餐后2小时血糖";
        } else {
            throw new RRException("项目编码有误");
        }
        testResultService.updateBs(id, testItemCode, testItemName);
        return new ApiRes();
    }
//    @Autowired
//    private TestResultWechatListener testResultWechatListener;
//
//    @ApiOperation(value = "test")
//    @GetMapping("test")
//    public R test(){
//        testResultWechatListener.NotTestBgWeek();
//        return new R();
//    }

}
